@extends('manage._layout')
@section('page-cssjs')
    <style type="text/css">
        .checkbox-inline {
            width: 150px;
            margin-left: 10px;
        }
        .box_left {
            width: 100%;
            margin-right: 50px;
        }
        .box_right {
            line-height: 22px;
            margin-top: 30px;
            padding-left: 45px;
        }

        .form-horizontal .control-label {
            padding-top: 7px;
            margin-bottom: 0;
            text-align: left;
        }
    </style>
@stop
@section('page-title')
    <h3>{{$name}}-权限列表</h3>
@stop
@section('page-breadcrumbs')
    <li>
        <a href="{{route('role_list')}}" title="">
            角色管理
        </a>
    </li>
    <li class="current">
        <a href="javascript:;" title="">
           权限列表
        </a>
    </li>
@stop
@section('page-buttons')
    <li>
        <a href="{{route('role_list')}}" title="">
            <i class="icon-reply"></i>
            <span>返回</span>
        </a>
    </li>
@stop

@section('page-content')
    <form class="form-horizontal" role="form" action="" enctype="multipart/form-data">
        <div class="form-group">

            @if(empty($roleMenuData))
                @foreach($menuData as $k=>$val)
                    <div class='box_left'>
                        <label for="name" class="col-sm-2 control-label" style="text-align: left">{{$val->name}}  <input type="checkbox" class="checkAll" value="{{$val->id}}" /></label>

                    </div>
                    <div class="box_right">
                        @foreach($val->menuModule as $res)
                            <label class="checkbox-inline">
                                <input type="checkbox" class='child_{{$val->id}}' name="menu_module"
                                       value="{{$val->id}}_{{$res->value}}">{{$res->module_name}}
                            </label>
                        @endforeach
                    </div>
                    </br>
                @endforeach
                @else
                @foreach($menuData as $k=>$val)
                    <div class='box_left'>
                        <label for="name" class="col-sm-2 control-label">{{$val->name}}  <input type="checkbox" class="checkAll" value="{{$val->id}}"/></label>

                    </div>
                    <div class="box_right">
                        @foreach($val->menuModule as $res)
                            <label class="checkbox-inline">
                                <input type="checkbox" class='child_{{$val->id}}' name="menu_module"
                          @foreach($roleMenuData as $row) @if($row->menu_id==$val->id &&(int)$row->power&(int)$res->value) checked="checked" @endif @endforeach   value="{{$val->id}}_{{$res->value}}">{{$res->module_name}}
                            </label>
                        @endforeach
                    </div>
                    </br>
                @endforeach
            @endif

        </div>

        <div class="form-group">
            <div class="col-sm-offset-2 col-sm-10">
                <button type="button" class="btn btn-primary" style='margin-left: -247px;' id="sub">保存</button>
            </div>
        </div>

        <input type="hidden" value="{{$roleId}}" id="roleId"/>
    </form>
    <script>
        $(document).ready(function () {
            $('#sub').click(function () {
                var arr = [];
                $('input[name="menu_module"]:checked').each(function () {
                    arr.push($(this).val());
                });
                var str = arr.join(",");

                var postdata = {
                    roleId: $('#roleId').val(),
                    power: str
                };

                $.ajax({
                    type: 'POST',
                    url: '{{URL::route('role_power_add_post')}}',
                    headers: { 'X-CSRF-TOKEN' : '{{ csrf_token() }}' },
                    data: postdata,
                    success: function (res) {
                        if (res.error == 0) {
                            history.go(-1);
                        } else {
                            layer.alert(res.message);
                        }
                    },
                    error: function(XMLHttpRequest) {
                        if(XMLHttpRequest.status==403){
                            layer.alert(noAccessMsg);
                        }else{
                            layer.alert(otherErrorMsg);
                        }
                    }
                });
            });
            //////////////////////////

            $(".checkAll").click(function(){
                var id=$(this).val();
                if(this.checked){
                    $(".child_"+id).prop("checked", true);
                }else{
                    $(".child_"+id).prop("checked", false);
                }
            });



        });
    </script>
@stop